package com.ph.irp.base.materialassistantattribute.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.ph.irp.base.materialassistantattribute.dao.AssistantAttributeDao;
import com.ph.irp.model.PBaseMaterialAssistantattribute;
import com.ph.util.DBUtil;

public class AssistantAttributeDaoImpl implements AssistantAttributeDao {

	private ComboPooledDataSource dataSource;

	public ComboPooledDataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(ComboPooledDataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public void addAssistantAttribute(PBaseMaterialAssistantattribute model)
			throws Throwable {
		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			pStmt = con
					.prepareStatement("insert into P_base_material_assistantattribute "
							+ "(AssistantAttributeTypeID,AssistantAttributeName,AssistantAttributeCode) "
							+ "values (?,?,?)");
			pStmt.setInt(1, model.getPBaseMaterialAssistantattributetypeId());
			pStmt.setString(2, model.getAssistantAttributeName());
			pStmt.setString(3, model.getAssistantAttributeCode());

			pStmt.executeUpdate();
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			DBUtil.setNullObject(con, pStmt, rs);
		}
	}

	@Override
	public void deleteAssistantAttribute(int id) throws Throwable {
		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			pStmt = con
					.prepareStatement("delete from P_base_material_assistantattribute where AssistantAttributeID = ?");
			pStmt.setInt(1, id);
			pStmt.executeUpdate();
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			DBUtil.setNullObject(con, pStmt, rs);
		}

	}

	@Override
	public List<PBaseMaterialAssistantattribute> getAllAssistantAttribute()
			throws Throwable {
		List<PBaseMaterialAssistantattribute> list = new ArrayList<PBaseMaterialAssistantattribute>();
		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		StringBuilder sb = new StringBuilder();
		sb.append("select * from P_base_material_assistantattribute where 1=1");
		try {
			con = dataSource.getConnection();
			pStmt = con.prepareStatement(sb.toString());
			rs = pStmt.executeQuery();

			while (rs.next()) {
				PBaseMaterialAssistantattribute pm = new PBaseMaterialAssistantattribute();
				pm.setAssistantAttributeId(rs.getInt("AssistantAttributeID"));
				pm.setAssistantAttributeCode(rs
						.getString("AssistantAttributeCode"));
				pm.setAssistantAttributeName(rs
						.getString("AssistantAttributeName"));
				pm.setPBaseMaterialAssistantattributetypeId(rs
						.getInt("AssistantAttributeTypeID"));

				list.add(pm);
			}
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}finally {
			try {
				DBUtil.setNullObject(con, pStmt, rs);
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}
		return list;
	}

	public List<PBaseMaterialAssistantattribute> getAssistantAttributeByCondition(
			PBaseMaterialAssistantattribute model) throws Throwable {
		List<PBaseMaterialAssistantattribute> list = new ArrayList<PBaseMaterialAssistantattribute>();
		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		StringBuilder sb = new StringBuilder();
		sb.append("select * from P_base_material_assistantattribute where 1=1");
		if (Integer.valueOf(model.getPBaseMaterialAssistantattributetypeId()) != null
				&& model.getPBaseMaterialAssistantattributetypeId() != 0) {
			sb.append(" and AssistantAttributeTypeID = ");
			sb.append(model.getPBaseMaterialAssistantattributetypeId());

		}
		if (model.getAssistantAttributeCode() != null
				&& !model.getAssistantAttributeCode().equals("")) {
			sb.append(" and AssistantAttributeCode like '%");
			sb.append(model.getAssistantAttributeCode());
			sb.append("%'");
		}
		if (model.getAssistantAttributeName() != null
				&& !model.getAssistantAttributeName().equals("")) {
			sb.append(" and AssistantAttributeName like '%");
			sb.append(model.getAssistantAttributeName());
			sb.append("%'");
		}
		try {
			con = dataSource.getConnection();
			pStmt = con.prepareStatement(sb.toString());
			rs = pStmt.executeQuery();

			while (rs.next()) {
				PBaseMaterialAssistantattribute pm = new PBaseMaterialAssistantattribute();
				pm.setAssistantAttributeId(rs.getInt("AssistantAttributeID"));
				pm.setAssistantAttributeCode(rs
						.getString("AssistantAttributeCode"));
				pm.setAssistantAttributeName(rs
						.getString("AssistantAttributeName"));
				pm.setPBaseMaterialAssistantattributetypeId(rs
						.getInt("AssistantAttributeTypeID"));

				list.add(pm);
			}
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}finally {
			try {
				DBUtil.setNullObject(con, pStmt, rs);
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}
		return list;
	}

	@Override
	public PBaseMaterialAssistantattribute getAssistantAttributeById(int id)
			throws Throwable {
		PBaseMaterialAssistantattribute model = new PBaseMaterialAssistantattribute();
		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			pStmt = con
					.prepareStatement("select * from P_base_material_assistantattribute where AssistantAttributeID = ?");
			pStmt.setInt(1, id);

			rs = pStmt.executeQuery();
			while (rs.next()) {
				model
						.setAssistantAttributeId(rs
								.getInt("AssistantAttributeID"));
				model.setAssistantAttributeCode(rs
						.getString("AssistantAttributeCode"));
				model.setAssistantAttributeName(rs
						.getString("AssistantAttributeName"));
				model.setPBaseMaterialAssistantattributetypeId(rs
						.getInt("assistantAttributeTypeID"));
			}
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			try {
				DBUtil.setNullObject(con, pStmt, rs);
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}
		return model;
	}
	
	public List<PBaseMaterialAssistantattribute> getAssistantattributeByTypeId(int assistantAttributeTypeId)throws Throwable{
		List<PBaseMaterialAssistantattribute> resList =  new ArrayList<PBaseMaterialAssistantattribute>();
		
		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			pStmt = con
					.prepareStatement("select * from P_base_material_assistantattribute where AssistantAttributeTypeID = ?");
			pStmt.setInt(1, assistantAttributeTypeId);

			rs = pStmt.executeQuery();
			while (rs.next()) {
				PBaseMaterialAssistantattribute model = new PBaseMaterialAssistantattribute();
				model
						.setAssistantAttributeId(rs
								.getInt("AssistantAttributeID"));
				model.setAssistantAttributeCode(rs
						.getString("AssistantAttributeCode"));
				model.setAssistantAttributeName(rs
						.getString("AssistantAttributeName"));
				model.setPBaseMaterialAssistantattributetypeId(rs
						.getInt("assistantAttributeTypeID"));
				resList.add(model);
			}
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			try {
				DBUtil.setNullObject(con, pStmt, rs);
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}
		return resList;
	}

	@Override
	public void updateAssistantAttribute(PBaseMaterialAssistantattribute model)
			throws Throwable {
		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			pStmt = con
					.prepareStatement("update P_base_material_assistantattribute "
							+ "set AssistantAttributeCode = ?,AssistantAttributename = ?,AssistantAttributeTypeID = ? "
							+ "where AssistantAttributeID = ?");
			pStmt.setString(1, model.getAssistantAttributeCode());
			pStmt.setString(2, model.getAssistantAttributeName());
			pStmt.setInt(3, model.getPBaseMaterialAssistantattributetypeId());
			pStmt.setInt(4, model.getAssistantAttributeId());
			pStmt.executeUpdate();
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			try {
				DBUtil.setNullObject(con, pStmt, rs);
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}
	}

}
